 /**
 * Description: 门店信息
 * @author:jacky
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-28   jacky       1.0.0     Initial
 */
package com.metro.demo.biz.service.impl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.metro.core.dao.IMapper;
import com.metro.core.excel.ExcelUtil;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.demo.biz.beans.vo.Biz4sInfoManage;
import com.metro.demo.biz.mapper.IBiz4sInfoManageMapper;
import com.metro.demo.biz.service.inf.IBiz4sInfoManageService;
import com.metro.demo.common.beans.constant.CommonConstant;
import com.metro.demo.common.beans.constant.DicTypeEnum;
import com.metro.demo.common.beans.constant.PropertiesEnum;
import com.metro.demo.common.utils.DicUtil;
import com.metro.demo.web.controller.imp.ImportResult;

 /**
 * 门店信息<br>
 *
 * @author:jacky
 * @since:1.0.0
 * @version [1.0.0, 2018-04-28]
 */
@Service("biz4sinfomanageService")
public class Biz4sInfoManageService extends BaseServiceSupport<Biz4sInfoManage> implements IBiz4sInfoManageService{
    //门店信息Mapper
    @Autowired
    private IBiz4sInfoManageMapper biz4sinfomanageMapper;

    @Override
    public IMapper getMapper() {
        return biz4sinfomanageMapper;
    }

    @Override
    public IResult save(Biz4sInfoManage entity) {
        try {
            biz4sinfomanageMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增门店信息失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(Biz4sInfoManage entity) {
        try {
            biz4sinfomanageMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改门店信息失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            biz4sinfomanageMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            biz4sinfomanageMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    /**
     * 导入
     */
    @Override
    public JsonResult  batchImport(String filePath){
        JsonResult jr = new JsonResult();
        ImportResult result = new ImportResult();

        String rootDir = PropertiesEnum.getPropertyValue(PropertiesEnum.FILE_UPLOAD_ROOT);
        String realPath = rootDir + "/" + filePath;

        Map data = new HashMap();
        List<Biz4sInfoManage> biz4sInfoManages = new ArrayList<>();
        
        data.put("biz4sInfoManages", biz4sInfoManages);
        try (InputStream excel = new FileInputStream(realPath)) {
            ExcelUtil.readByTemplate("/template/biz/biz4sinfoManage.xml", data, excel);

            List<Biz4sInfoManage> importBiz4sInfoManages = new ArrayList<Biz4sInfoManage>();
            for (Biz4sInfoManage biz4sInfoManage : biz4sInfoManages){
            	biz4sInfoManage.setDataStatus(CommonConstant.YES);
            	//导入的手机号格式转化
            /*	try {
	            		String im = biz4sInfoManage.getItMobile();
	                	String sm = biz4sInfoManage.getSdMobile();
	                	DecimalFormat format = new DecimalFormat("#");
	                	if(im !=null && im != "") {
	                		Number itMoblie= (Number)new BigDecimal(im);
	                		biz4sInfoManage.setItMobile(format.format(itMoblie));
	                	}
	                	if(sm !=null && sm != "") {
	                		Number sdMobile= (Number)new BigDecimal(sm);
	                		biz4sInfoManage.setSdMobile(format.format(sdMobile));       
                	}
				} catch (Exception e) {
					jr.setResult(ResultCode.ERROR);
				}*/
            	//批次匹配
            	String count = biz4sInfoManage.getCount();
            	Long bid = DicUtil.getId(DicTypeEnum.COUNT, count);
            	biz4sInfoManage.setCount(String.valueOf(bid));

               /* biz4sInfoManage.setImplTime(biz4sInfoManage.getImplTime());
                biz4sInfoManage.setServiceBegeintime(biz4sInfoManage.getServiceBegeintime());
                biz4sInfoManage.setServiceEndintime(biz4sInfoManage.getServiceEndintime());*/

               // this.save(biz4sInfoManage);
               // 将数据存入list中批量保存
               importBiz4sInfoManages.add(biz4sInfoManage);
            }
            //循环完批量保存
            biz4sinfomanageMapper.saveImportList(importBiz4sInfoManages);

            result.setTotalNum(biz4sInfoManages.size());
            jr.setResult(ResultCode.SUCCESS);
            jr.setData(result);
        }catch (Exception ex){
            log.error("读取文件失败", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

     /**
      * 格式化导入的日期
      * @param str
      * @return
      */
    public Date dateFormat(String str){
        System.out.println(str);
        Date date = null;
        if(str != null && str != ""){
            String[] ss = str.split("/");
            String time = ss[0]+"-"+ss[1]+"-"+ss[2];
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = sdf.parse(time);
                System.out.println("=========="+date+"=================");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;

    }

     public static void main(String[] args) {
         Date date = null;
        String str = "2016/5/31";
         String[] ss = str.split("/");
         String time = ss[0]+"-"+ss[1]+"-"+ss[2];
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         try {
             date = sdf.parse(time);
             System.out.println("=========="+date+"=================");
         } catch (ParseException e) {
             e.printStackTrace();
         }
     }
}